package com.foodndate.food.member;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foodndate.food.taste.TasteDao;
import com.foodndate.food.taste.TasteDto;



@Controller
@RequestMapping("/member")
public class MemberController {
	
	@Autowired private MemberDao memberdao;
	@Autowired private TasteDao tastedao;
	/*@Autowired private TasteDto tastedto;*/
	//메인
	@RequestMapping(value="/" ,method=RequestMethod.GET)
	public String main(){
		
		System.out.println("main");
		return "member/main";			
	}
	//회원가입폼이동
	@RequestMapping(value="/join" ,method=RequestMethod.GET)
	public String join(){
		return "member/join";			
	}
	//회원가입 처리
	@RequestMapping(value="/joinprocess",method=RequestMethod.GET)
	public String joinprocess(MemberDto memberdto,TasteDto tastedto){
		System.out.println("회원가입 컨트롤 join");
		
		System.out.println(memberdto.getMember_id());
		int b=tastedao.chk(memberdto.getMember_id());
		System.out.	println("foodid : " +b);
		int a=memberdao.chk(memberdto.getMember_id());
		System.out.println("sssss");
		if(a ==0){
			System.out.println("ssss");
			if(b==0){
			System.out.println("사용가능아이디 ");
			}else {
				System.out.println("사용 불가능 아이디");
		     return "member/main";
			}
		}else{
		System.out.println("사용불가능 아이디");
		return "member/main";
		}
		memberdao.insert(memberdto);
		return "member/main";
	}
	
	//id체크폼 이동후 아이디 중복 체크
	@RequestMapping(value="/chk",method=RequestMethod.GET)
	public String chk(String id,MemberDto memberdto,TasteDto tastedto){
		System.out.println("중복확인 폼이동 확인");
		System.out.println("클라이언트 id값 : " +id);
		/*System.out.println("id : " +member_id );
		memberdto.setMember_id(member_id);	*/
		int b=tastedao.chk(id);
		System.out.	println("foodid : " +b);
		int a=memberdao.chk(id);
		System.out.println("sssss");
		if(a ==0){
			System.out.println("ssss");
			if(b==0){
			System.out.println("사용가능아이디 ");
			}else {
				System.out.println("사용 불가능 아이디");
			}
		}else{
		System.out.println("사용불가능 아이디");
		}
		return "member/chk";
				
	}
	//로그인 페이지 이동
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		System.out.println("로그인 페이지");
	return "member/login";
	}
	
	//로그인 프로세스
	@RequestMapping(value="/loginprocess",method=RequestMethod.GET)
	public String loginprocee(String login_id,String login_pw,MemberDto memberdto,HttpSession session, Model model){
		memberdto.setMember_id(login_id);
		memberdto.setMember_pw(login_pw);
		int su=memberdao.login(memberdto);
		System.out.println("로그인 pw : " + memberdto.getMember_pw());
		System.out.println("su : " + su);
		System.out.println("");
		model.addAttribute("su", su);
		
		
		if(su==1){session.setAttribute("sessionId", memberdto.getMember_id());
			if(memberdto.getMember_id().equals("admin")){
				System.out.println("관리자");
				return "member/test";
			}
			return "member/test";
		}else{
			model.addAttribute("su","아이디 ./ 비밀번호 확인요청");
			return "member/login";
		}
		
//		
//		if(memberdto.getMember_id() != null ){
//			if(pw.equals(memberdto.getMember_pw())){
//				System.out.println("로그인 성공");
//				session.setAttribute("sessionId", memberdto.getMember_id());	
//			}else{
//				System.out.println("비밀번호를 확인해 주세요");
//				return "member/login";
//			}
//		}else{
//			System.out.println("아이디를 확인해 주세요");
//		}		
//		return "member/test";
	}
	//수정전 회원정보 불러오기
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public String detail(HttpSession session,MemberDto memberdto,Model model){

		System.out.println("디테일");
		String id=(String)session.getAttribute("sessionId");
		System.out.println("세션 겟 아이디  : " + id);
		memberdto.setMember_id(id);
		
		memberdto=memberdao.info(memberdto);
		System.out.println("detail : info" + memberdto.getMember_id());
		System.out.println("detail : info" + memberdto.getMember_pw());
		System.out.println("detail : info" + memberdto.getMember_name());
		model.addAttribute("detail", memberdto);
				
		return "member/update";
	}
	@RequestMapping(value="/updateprocess",method=RequestMethod.GET)
	public String updateprocess(MemberDto memberdto){
		System.out.println("update됨");
		int su = memberdao.updateprocess(memberdto);
		if(su == 0 ){
			System.out.println("업데이트 실패");
		}else{
			System.out.println("업데이트 성공");
		}
		return "member/test";
	}
	//
	@RequestMapping(value="/smallchk" ,method=RequestMethod.GET)
	public String smallchk(){
		
		System.out.println("smallchk");
		return "member/smallchk";			
	}
	//회원 탈퇴
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String delete(HttpSession session,MemberDto memberdto){
		String id=(String) session.getAttribute("sessionId");
		memberdto.setMember_id(id);
		memberdao.delete(memberdto);
		return "member/main";
	}
	//회원 로그인 화면
	@RequestMapping(value="/joinus",method=RequestMethod.GET)
	public String joinus(HttpSession session,MemberDto memberdto){
		
		return "member/joinus";
	}
	//일반회원 메인화면
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public String test(){
		
		return "member/test";
	}
	//회원관리 리스트
	@RequestMapping(value="/memberlist",method=RequestMethod.GET)
	public String list(MemberDto memberdto,Model model){
		List memberlist=memberdao.list(memberdto);
		System.out.println("멤버 리스트" + memberlist.size());
		model.addAttribute("memberlist", memberlist);
		return "member/list";
	}
	//어드민 회원 삭제
	@RequestMapping(value="/admindel",method=RequestMethod.GET)
	public  String admindel(int num){
		System.out.println("삭제할 번호"+num);
		memberdao.admin(num);
		return "redirect:memberlist";
	}

}
